package com.maozzi.service.acl.domain.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maozzi.service.acl.domain.UserRoleDomainService;
import com.maozzi.service.acl.repository.mapper.UserRoleMapper;
import com.maozzi.service.acl.repository.model.UserRole;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 权限 - 用户角色 domain service
 *
 * @author maozi
 */
@Service
public class UserRoleDomainServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleDomainService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<UserRole> getUserRole(Long userId) {
        return userRoleMapper.selectList(Wrappers.lambdaQuery(UserRole.class).eq(UserRole::getUserId, userId));
    }

    @Override
    public Boolean delUserRoleBatch(Long userId) {
        return userRoleMapper.delete(Wrappers.lambdaQuery(UserRole.class).eq(UserRole::getUserId, userId)) > 0;
    }

    @Override
    public Boolean putUserRole(Long userId, Long roleId) {
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        return userRoleMapper.insert(userRole) > 0;
    }


    @Override
    public Boolean putUserRoleBatch(Long userId, Long... roleIds) {
        for (Long roleId : roleIds) {
            Boolean isPut = putUserRole(userId, roleId);
            if (!isPut) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }
}
